clear all
set more off
***Set CD here
log using "MechProject.log", replace
*** Import CSM_filter.csv here

ssc install estout
*************************************************
** 			   Creating New Variables		   **
*************************************************
***Generating dummy variables for heavy and light based on the weighted district dismount ratio.
generate heavy = 1 if mat_unit_mech_weighted<10.9999
replace heavy = 0 if mat_unit_mech_weighted>11
generate SIG_Lag = real(sig_slide_26wk)
generate lnsig_1 = ln(sig_1)
generate percapSIG = p_s1/100
generate logsig_1 = ln(sig_1+1)

***set Stata to work with panel data; district-week unit of analysis
xtset dist week

***Creating Table 1: Descriptive Statistics of Selected Variables
label var heavy				      	"Armored Unit"
label var sig_1						"Current Dist.-Week SIGACTs"
label var SIG_Lag					"6 mo. SIGACTs Lag"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var tforce					"Troop Density"
label var urate						"Unemp. Rate"
label var pop_den					"Pop. Density"
label var su_v						"Sunni Share"
label var sh_v						"Shia Share"
label var k_v						"Kurd Share"
eststo clear
estpost tabstat sig_1 SIG_Lag mat_unit_mech_weighted heavy tforce urate pop_den su_v sh_v k_v, statistics(n mean sd p25 p50 p75 p95 max) columns(statistics)
esttab using "Sumstats.tex", cells("n(fmt(a2) label(Observations)) mean(fmt(a2) label(Mean)) sd(fmt(a2) label(Std.\ Dev.)) p25(fmt(a2) label(25\%)) p50(fmt(a2) label(50\%)) p75(fmt(a2) label(75\%))  p95(fmt(a2) label(95\%)) max(fmt(a2) label(Max.))") ///
	nostar nonumbers nomtitle label booktabs width(38em) replace
eststo clear
eststo clear

***Creating Table 2: Regression Results of SIGACTs on Dismount Ratio, Armoured Unit and Controls
label var percapSIG					"Per Capita SIGACTs"
label var heavy				      	"Armored Unit"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var sig_1						"Current Dist.-Week SIGACTs"
label var SIG_Lag					"6 mo. SIGACTs Lag"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var tforce					"Troop Density"
label var urate						"Unemp. Rate"
label var pop_den					"Pop. Density"
label var su_v						"Sunni Share"
label var sh_v						"Shia Share"
label var k_v						"Kurd Share"
eststo clear
eststo: zinb sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: zinb sig_1 heavy tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(heavy tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: xtreg p_s1 mat_unit_mech_weighted tforce pop_den urate i.year SIG_Lag, fe robust cluster(district)
eststo: xtreg p_s1 heavy tforce pop_den urate i.year SIG_Lag, fe robust cluster(district)
esttab using MechModels.tex, cells(b(star fmt(3)) se(par fmt(2)))   ///
legend label varlabels(_cons constant)              				 ///
stats(n aic, fmt(3 0 1) label(Obs AIC))					///
title(Count and Fixed Effects Regression Results of SIGACTs on Dismount Ratio and Controls)		///
addnote(Values in parenthesis are robust standard errors clustered on district.) 
eststo clear


**************************************************************
**           Online Supplemental Index                      **
**************************************************************	
*** Table 2: See separate Stata .do file and .dta file for Iraqi Security Forces OLS Regression
*** Table 4: Robustness Checks; OLS and Fixed Effects Regression Results of log(SIGACTs+1) on Dismount Ratio and Controls
eststo: reg lnsig_1 tforce mat_unit_mech_weighted pop_den urate SIG_Lag, robust
outreg2 using MechModels.xls, append label ctitle(OLS)
eststo: xtreg lnsig_1 mat_unit_mech_weighted, fe cluster(district)
outreg2 using MechModels.xls, append label ctitle(FE-1)
eststo: xtreg lnsig_1 mat_unit_mech_weighted tforce, fe cluster(district)
outreg2 using MechModels.xls, append label ctitle(FE-2)
eststo: xtreg lnsig_1 mat_unit_mech_weighted tforce pop_den urate, fe cluster(district)
outreg2 using MechModels.xls, append label ctitle(FE-3)
eststo: xtreg lnsig_1 mat_unit_mech_weighted tforce pop_den urate SIG_Lag, fe cluster(district)
outreg2 using MechModels.xls, append label ctitle(FE-4)
eststo: areg lnsig_1 mat_unit_mech_weighted tforce pop_den urate SIG_Lag i.year, absorb(district) cluster(district)
outreg2 using MechModels.xls, append label ctitle(FE-5)
esttab using MechModels2.tex, se ar2 aic label title(Log(SIGACTs) Local Dismount Ratio Models: OLS and Fixed Effects)addnote(N is reduced by 2185 observations using a logged SIGACTs dependent variable as all 0 SIGACTs districts are removed from the model.)
eststo clear
*** Table 5: Robustness Checks; Regression Results of SIGACTs on Dismount Ratio, Armoured Unit and Controls
label var percapSIG					"Per Capita SIGACTs"
label var heavy				      	"Armored Unit"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var sig_1						"Current Dist.-Week SIGACTs"
label var SIG_Lag					"6 mo. SIGACTs Lag"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var tforce					"Troop Density"
label var urate						"Unemp. Rate"
label var pop_den					"Pop. Density"
label var su_v						"Sunni Share"
label var sh_v						"Shia Share"
label var k_v						"Kurd Share"
eststo clear
eststo: nbreg sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, robust cluster(district)
eststo: nbreg sig_1 heavy tforce su_v sh_v pop_den urate i.year SIG_Lag, robust cluster(district)
eststo: zinb sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year) robust cluster(district)
eststo: zinb sig_1 heavy tforce su_v sh_v pop_den urate i.year, inflate(heavy tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: xtreg p_s1 mat_unit_mech_weighted tforce pop_den urate i.year, fe robust cluster(district)
eststo: xtreg p_s1 heavy tforce pop_den urate i.year, fe robust cluster(district)
esttab using MechAppendix.tex, cells(b(star fmt(3)) se(par fmt(2)))   ///
legend label varlabels(_cons constant)              				 ///
stats(n aic, fmt(3 0 1) label(Obs AIC))					///
title(Count and Fixed Effects Regression Results of SIGACTs on Dismount Ratio and Controls)		///
addnote(Values in parenthesis are robust standard errors clustered on district.) 
eststo clear
***Table 6 Robustness Checks; Count Model Regression Results of SIGACT Types on Armored Units and Controls
label var percapSIG					"Per Capita SIGACTs"
label var heavy				      	"Armored Unit"
label var sig_1						"Current Dist.-Week SIGACTs"
label var SIG_Lag					"6 mo. SIGACTs Lag"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var tforce					"Troop Density"
label var urate						"Unemp. Rate"
label var pop_den					"Pop. Density"
label var su_v						"Sunni Share"
label var sh_v						"Shia Share"
label var k_v						"Kurd Share"
eststo clear
eststo: zinb ied mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: zinb df mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: zinb idf mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
eststo: zinb suicide mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
esttab using App2MechModels.tex, cells(b(star fmt(3)) se(par fmt(2)))   ///
legend label varlabels(_cons constant)              				 ///
stats(se ar2 aic bic) 					///
title(Count Model Regression Results of SIGACT Types on Dismount Ratio and Controls)		///
addnote(Values in parenthesis are robest standard errors clustered on district.) 
eststo clear

***Table 7: Robustness Checks;  Determinants of Armored Unit Deployments, Logit Models (Online Supplemental Index)
label var percapSIG					"Per Capita SIGACTs"
label var heavy				      	"Armored Unit"
label var sig_1						"Current Dist.-Week SIGACTs"
label var SIG_Lag					"6 mo. SIGACTs Lag"
label var mat_unit_mech_weighted	"Dismount Ratio"
label var tforce					"Troop Density"
label var urate						"Unemp. Rate"
label var pop_den					"Pop. Density"
label var su_v						"Sunni Share"
label var sh_v						"Shia Share"
label var k_v						"Kurd Share"
eststo: logit heavy sig_1 tforce su_v sh_v pop_den urate i.year, robust cluster(district)
eststo: logit heavy l4.sig_1 tforce su_v sh_v pop_den urate i.year, robust cluster(district)
eststo: logit heavy l8.sig_1 tforce su_v sh_v pop_den urate i.year, robust cluster(district)
eststo: logit heavy l12.sig_1 tforce su_v sh_v pop_den urate i.year, robust cluster(district)
esttab using MechCIVCASEndog.tex, cells(b(star fmt(3)) se(par fmt(2)))   ///
legend label varlabels(_cons constant)              				 ///
stats(se ar2 aic, fmt(3 0 1) label(R-sqr SE AIC))					///
title(Determinants of Armored Unit Deployments, Logit Models)		///
addnote(Values in parenthesis are robest standard errors clustered on district.) 
eststo clear

***Table 8: See separate .R file for OrthoPanels test



**************************************************************************
**        ZIP and Vuong Testing for ZINB models             **
**************************************************************************	
**Poisson Regression Model (PRM)
eststo: poisson sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, robust cluster(district)
estimates store pois
**Zero Inflated Poisson Regression (ZIP)
eststo: zip sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted su_v sh_v tforce pop_den urate i.year SIG_Lag) robust cluster(district) 
estimates store zip
estimates stats pois zip 
***Lower AIC/BIC for ZIP indicate that model is prefered to the Poisson Regression. 
**Negative Binomial Regression (NBR)
eststo: nbreg sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, robust cluster(district)
estimates store nbr
**Zero Inflated Binomial Regression (ZINB)
eststo: zinb sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate( mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) robust cluster(district)
estimates store zinb
estimates stats nbr zinb
***Lower AIC/BIC for ZINB indicate that model is prefered to the NBR. 
*Now running a ZINB with a Vuong test to determine if ZINB or Non-Zero Inflated model
zinb sig_1 mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag, inflate(mat_unit_mech_weighted tforce su_v sh_v pop_den urate i.year SIG_Lag) forcevuong
***Z=18.81 indicating that zero inflated model is ideal. 
esttab using App1MechModels.tex, cells(b(star fmt(3)) se(par fmt(2)))   ///
legend label varlabels(_cons constant)              				 ///
stats(se ar2 aic bic) 					///
title(Count Model Regression Results of SIGACTs on Dismount Ratio and Controls)		///
addnote(Values in parenthesis are robest standard errors clustered on district.) 
eststo clear


*************************************************
** 			Hausmann Test			   **
*************************************************
**Hausmann Test for Fixed or Random Effects
**If "Prob>chi2" is < 0.05, then fixed effects model is appropriate.
eststo: xtreg percapSIG tforce mat_unit_mech_weighted pop_den urate SIG_Lag
estimates store random
eststo: xtreg percapSIG tforce mat_unit_mech_weighted pop_den urate SIG_Lag, fe
estimates store fixed 
hausman fixed random
esttab using Hausman.tex
eststo clear


*****************************************************
** 			   Panel Unit Root Testing			   **
*****************************************************
xtunitroot fisher sig_1, pperron lags(2)
xtunitroot fisher percapSIG, pperron lags(2)
**Fisher Tests show that SIGACTs data is stationary (p-value=0.0000). Thus we reject the null hypothesis that that all panels contain unit roots. 
**Anderson-Hsiao levels estimator
ivreg d.percapSIG (d.SIG_Lag=l.SIG_Lag) d.(mat_unit_mech_weighted tforce pop_den urate)


log off 
translate "MechProject.log" "MechProject.pdf"

